package com.zrgj.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zrgj.domain.ProductPo;
import com.zrgj.domain.UserCollection;
import com.zrgj.manager.mapper.ProductMapper;
import com.zrgj.manager.mapper.UserCollectionMapper;
import com.zrgj.manager.service.IUserCollectionService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Service
@Component
public class UserCollectionService implements IUserCollectionService {
    private UserCollectionMapper userCollectionMapper;
    private ProductMapper productMapper;

    public UserCollectionService(UserCollectionMapper userCollectionMapper, ProductMapper productMapper) {
        this.userCollectionMapper = userCollectionMapper;
        this.productMapper = productMapper;
    }

    @Override
    public int insert(UserCollection userCollection) {
        UserCollection collection = userCollectionMapper.selectCollection(userCollection);
        if (collection != null){
            return -1;
        }
        return userCollectionMapper.insert(userCollection);
    }

    @Override
    public void delete(Integer collectionId) {
        userCollectionMapper.delete(collectionId);
    }

    @Override
    public List<UserCollection> select(Integer userId) {
        List<UserCollection> select = userCollectionMapper.select(userId);
        List<UserCollection> list = new ArrayList<>();

        for (UserCollection collection: select){
            ProductPo productPo = productMapper.selectVo(collection.getProductId());
            collection.setProductPo(productPo);
            list.add(collection);
        }
        return list;
    }


}
